<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	private $_id;
	const ERROR_EMAIL_UNCHECK=10;
	/**
	 * Authenticates a user.
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate()
	{
		$admin = Admin::model()->find("a_username='{$this->username}' and a_isdel ='1'");
		if($admin===null)
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		else if(md5($this->password)!=$admin->a_password){
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
		}else{
			$this->_id=$admin->a_id;
			$this->setState("adminisGuest",'yes');
			$this->setState("studentisGuest",'');
			$admin->a_loginnum = $admin->a_loginnum+1;
			
			$admin->update();
			if($admin->a_loginnum==1){
				$cookie = Yii::app()->request->getcookies();
			}
			$this->errorCode=self::ERROR_NONE;
		}
			
		return $this->errorCode==self::ERROR_NONE;
	}

	/**
	 * @return integer the ID of the admin record
	 */
	public function getId()
	{
		return $this->_id;
	}
}